package com.ellricky.tv.util

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import com.andr.common.http.dialog.ProgressDialog
import com.andr.common.http.util.JsonUtil
import com.andr.common.http.util.Util
import com.andr.common.util.ToastUtil
import com.ellricky.tv.ui.TvLoginActivity
import com.zhy.http.okhttp.callback.StringCallback

import okhttp3.Call
import okhttp3.Request
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

abstract class TvBaseCallback : StringCallback {

    var showLoading = false
    var mContext: Activity? = null
    var mDialog: ProgressDialog? = null
    var loadMsg: String? = null
    var canCancel = false

    constructor(context: Activity?) : this(context, true)

    constructor(context: Activity?, showLoading: Boolean) {
        canCancel = true
        this.showLoading = showLoading
        mContext = context
    }

    constructor(context: Activity?, showLoading: Boolean, loadMsg: String?) {
        canCancel = true
        this.showLoading = showLoading
        mContext = context
        this.loadMsg = loadMsg
    }

    fun setDialogCancel(cancel: Boolean) {
        canCancel = cancel
    }

    override fun onBefore(request: Request?, id: Int) {
        super.onBefore(request, id)
        showDialog()
    }

    override fun onError(call: Call?, e: Exception?, id: Int) {
        var msg: String? = "未知错误"
        if (e != null) {
            val message = e.message
            if (!TextUtils.isEmpty(message)) {
                msg = message
            }
        }
        onHttpError("-1000", msg)
    }

    override fun onAfter(id: Int) {
        super.onAfter(id)
        dismissDialog()
    }

    private fun showDialog() {
        if (showLoading && mContext != null && !mContext!!.isFinishing) {
            if (mDialog == null) {
                mDialog = if (TextUtils.isEmpty(loadMsg)) {
                    ProgressDialog(mContext)
                } else {
                    ProgressDialog(mContext, loadMsg)
                }
                mDialog!!.setCancelable(canCancel)
                mDialog!!.setCanceledOnTouchOutside(canCancel)
            }
            if (!mDialog!!.isShowing) {
                mDialog!!.show()
            }
        }
    }

    private fun dismissDialog() {
        if (mDialog != null && mDialog!!.isShowing) {
            mDialog!!.dismiss()
        }
    }

    override fun onResponse(response: String?, id: Int) {
        try {
            val resultJO = Util.convertResponse(response)
            val httpCode = resultJO.optString("httpCode")
            val msg = resultJO.optString("msg")
            val retCode = resultJO.optString("retCode")
            val retMsg = resultJO.optString("retMsg")
            val timeStamp = resultJO.optString("timestamp")
            val data = resultJO.optString("data")
            if ("200" == httpCode) {
                when (dataFlag()) {
                    1 -> {
                        val dataJO = JsonUtil.parseJO(data)
                        parseDataJO(retCode, retMsg, dataJO)
                    }
                    2 -> {
                        val dataJA = JsonUtil.parseJA(data)
                        parseDataJA(retCode, retMsg, dataJA)
                    }
                    else -> {
                        parseDataStr(retCode, retMsg, data)
                    }
                }
                dealTimeStamp(retCode, timeStamp)
            } else {
                if ("801" == httpCode) {
                    if (mContext != null) {
                        val intent = Intent(mContext, TvLoginActivity::class.java)
                        intent.putExtra("fromMain", true)
                        mContext?.startActivity(intent)
                        mContext?.finish()
                    }
                    return
                }
                onHttpError(httpCode, msg)
            }
        } catch (var12: JSONException) {
            onError(null as Call?, var12, -1)
        }
    }

    fun dealTimeStamp(retCode: String?, timeStamp: String?) {}

    abstract fun dataFlag(): Int

    fun onHttpError(code: String?, msg: String?) {
        ToastUtil.show(mContext, msg)
    }

    open fun parseDataJO(code: String?, msg: String?, dataJO: JSONObject?) {}

    fun parseDataJA(code: String?, msg: String?, dataJA: JSONArray?) {}

    fun parseDataStr(code: String?, msg: String?, dataStr: String?) {}
}